package com.sws.controller;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sws.Constant;
import com.sws.model.bean.B2SPaperReviewBean;
import com.sws.model.bean.C2SPageBean;
import com.sws.model.bean.C2SPaperBean;
import com.sws.service.PaperService;
import com.sws.utils.CUtils;

@Controller
public class PaperController {

	private Logger logger = LoggerFactory.getLogger(PaperController.class);
	private static final String LOCATION_PATH = "C:/ServerUpload/SWSUpload/image";
	private static final String REQUEST_PATH = "SWSUpload/image/";
//	private static final String LOCATION_PATH = "E:/serverfile/SWSUpload/image";
	
	@Autowired
	private PaperService service;
	
	@ResponseBody
	@RequestMapping("/s/uploadPaper")
	public String uploadPaper(C2SPaperBean paper, HttpServletRequest request) {
		JSONObject result = new JSONObject();
		logger.info("=====> 上传论文信息及证明");
		String fileName = "paper_" + CUtils.getDateTime() + "_" + paper.getFile().getOriginalFilename();
		File dir = new File(LOCATION_PATH);
		//判断上传文件的保存目录是否存在
		if (!dir.exists() && !dir.isDirectory()) {
			logger.info("创建目录");
			dir.mkdirs();
		}
		try {
			File saveFile = new File(LOCATION_PATH, fileName);
			paper.getFile().transferTo(saveFile);
			paper.setDocument(REQUEST_PATH+fileName);
			service.uploadPaper(paper);
			if (paper.getIsSuccess()) {
				result.put("status", Constant.INSTANSE.OK);
				result.put("msg", "上传成功！");
			} else {
				result.put("status", Constant.INSTANSE.FAIL);
				result.put("msg", "上传失败，请检查！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/s/getPersonalPaper")
	public String getPersonalPaper(C2SPageBean pageBean) {
		logger.info("=====> 获取个人论文信息");
		JSONObject result = new JSONObject();
		
		try {
			result.put("status", Constant.INSTANSE.OK);
			result.put("data", service.getPersonalPaper(pageBean));
			result.put("msg", "获取个人论文信息成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/paperReview")
	public String paperReview(B2SPaperReviewBean bean) {
		logger.info("=====> 审核论文信息");
		JSONObject result = new JSONObject();

		try {
			service.paperReview(bean);
			result.put("status", Constant.INSTANSE.OK);
			result.put("msg", "审核论文信息成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		
		return result.toJSONString();
	}
}
